package cn.xshi.sys.service.impl;

import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.log.client.service.LogsUtil;
import cn.xshi.sys.dao.SysUserInfoDao;
import cn.xshi.sys.model.SysUserInfo;
import cn.xshi.sys.service.SysUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Desc 员工信息
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Slf4j
@Service
public class SysUserInfoServiceImpl extends BaseService implements SysUserInfoService {

	@Resource
    SysUserInfoDao sysUserinfoDao;

	@Resource
    LogsUtil logsUtil;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<SysUserInfo> getSysUserInfoListByCondition(Map<String,Object> condition){
		try {
			return sysUserinfoDao.getSysUserInfoListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public SysUserInfo getSysUserInfoById(String id){
		try {
			return sysUserinfoDao.getSysUserInfoById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param sysUserinfo
	* @return
	*/
	public int addSysUserInfo(SysUserInfo sysUserinfo){
		int i = 0;
		try {
			sysUserinfo.setCreateId(getCurrentAccountId());
			sysUserinfo.setCreateTime(getDate());
			i = sysUserinfoDao.addSysUserInfo(sysUserinfo);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("用户业务类", "添加", "执行添加操作");
		} catch (Exception e) {
			logsUtil.aBLogs("用户业务类", "添加", "执行添加操作--失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param sysUserinfo
	* @return
	*/
	public int updateSysUserInfo(SysUserInfo sysUserinfo){
		int i = 0;
		try {
			SysUserInfo before = sysUserinfoDao.getSysUserInfoById(sysUserinfo.getId());
			sysUserinfo.setUpdateId(getCurrentAccountId());
			sysUserinfo.setUpdateTime(getDate());
			i = sysUserinfoDao.updateSysUserInfo(sysUserinfo);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("用户业务类", "修改", "执行修改操作");
			//记录字段变更日志
			logsUtil.aRecord(before, sysUserinfo, "Xt_Userinfo", sysUserinfo.getId());
		} catch (Exception e) {
			logsUtil.aBLogs("用户业务类", "修改", "执行修改操作--失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delSysUserInfo(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysUserinfoDao.delSysUserInfo(condition);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("用户业务类", "删除", "执行删除操作");
		} catch (Exception e) {
			logsUtil.aBLogs("用户业务类", "删除", "执行删除操作--失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 根据账号查找对象
	 * @param account
	 * @return
	 */
	public SysUserInfo getSysUserInfoByUserName(String account){
		return sysUserinfoDao.getSysUserInfoByUserName(account);
	}
	/**
	 * 读取所有用户根据各种情况非分页
	 * @param condition
	 * @return
	 */
	public List<SysUserInfo> getSysUserInfoListAllByCondition(Map<String,Object> condition){
		try {
			return sysUserinfoDao.getSysUserInfoListAllByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 验证用户名是否存在
	 * @return
	 */
	public int validateUser(Map<String,Object> condition){
		try {
			return sysUserinfoDao.validateUser(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 已删除用户
	 * @param condition
	 * @return
	 */
	public List<SysUserInfo> getSysUserInfoDeletedListByCondition(Map<String,Object> condition){
		try {
			return sysUserinfoDao.getSysUserInfoDeletedListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
	
	/**
	 * 恢复用户
	 * @param condition
	 * @return
	 */
	public int recoverSysUserInfo(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysUserinfoDao.recoverSysUserInfo(condition);
			//统一推送
			addPushDataAuthority();
			logsUtil.aBLogs("用户业务类", "恢复用户", "执行恢复用户操作");
		} catch (Exception e) {
			logsUtil.aBLogs("用户业务类", "恢复用户", "执行恢复用户操作--失败");
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 根据各种情况查找集合不分页（流程设计器中处理人 发起人 发起人组及同步数据之授权中心等使用）
	 * @param condition
	 * @return
	 */
	public List<SysUserInfo> getSysUserInfoList(Map<String,Object> condition){
		try {
			return sysUserinfoDao.getSysUserInfoList(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}
}
